<template>
  <!-- 左侧二级菜单栏的组件封装 -->
  <div class="sidebar">
    <el-menu class="sidebar-el-menu" :default-active="toIndex" background-color="#fff" text-color="#000000" router>
        <div v-for="item in itemList" :key="item.path">
            <!-- 二级菜单 -->
            <el-menu-item :index="item.path" :key="item.path" v-if="item.children === undefined">
                <span slot="title">{{item.name}}</span>
            </el-menu-item>
            <!-- 三级菜单 -->
            <el-submenu :index="item.path" v-else>
                <span slot="title">{{item.name}}</span>
                <el-menu-item-group>
                    <el-menu-item v-for="subItem in item.children" :index="subItem.path" :key="subItem.path">
                        {{subItem.name}}
                    </el-menu-item>
                </el-menu-item-group>
            </el-submenu>
        </div>
    </el-menu>
  </div>
</template>

<script>
export default {
    props: {
        itemList: {
            type: Array,
            default: () => []
        }
    },
    computed: {
        // 根据路径绑定到对应的二级菜单，防止页面刷新重新跳回第一个
        toIndex(){
          return this.$route.path.split('/')[2];
        }
    },
}
</script>

<style lang="scss" scoped>
/* 左侧菜单栏定位和位置大小设定 */
.sidebar {
  display: block;
  position: absolute;
  left: 0;
  top: 70px;
  bottom: 0;
  overflow-y: scroll;
}
.sidebar::-webkit-scrollbar {
  width: 0;
}
.sidebar-el-menu {
  width: 250px;
}
.sidebar > ul {
  height: 100%;
}

/* 左侧二级菜单项的样式 */
.el-submenu .el-menu-item{
  font-size: 14px !important;
  padding-left: 35px !important;
}

/* 左侧二级菜单选中时的样式 */
.el-menu-item.is-active {
  color: white !important;
  background: #3989fa !important;
}
.el-menu-item, .el-submenu__title {
  height: 50px !important;
  line-height: 50px !important;
}
/* 隐藏分组标题 */
.el-menu-item-group__title {
    display: none;
}
</style>
